package com.huanwang.commonlibrary.utils

import java.lang.reflect.ParameterizedType

/**
 * 反射类
 */
object ReflectUtils {

    /**
     * 返回此类中泛型的类型
     */
    fun <T> getInstanceToName(`object`: Any?, i: Int): Class<T>? {
        return if (`object` != null) {
            (`object`.javaClass
                .genericSuperclass as ParameterizedType)
                .actualTypeArguments[i] as Class<T>
        } else null
    }



    /**
     * 返回此类中泛型的类型的实例
     */
     fun <T> getNewInstance(`object`: Any?, i: Int): T? {
        if (`object` != null) {
            try {
                return ((`object`.javaClass
                    .genericSuperclass as ParameterizedType).actualTypeArguments[i] as Class<T>)
                    .newInstance()
            } catch (e: InstantiationException) {
                e.printStackTrace()
            } catch (e: IllegalAccessException) {
                e.printStackTrace()
            } catch (e: ClassCastException) {
                e.printStackTrace()
            }
        }
        return null
    }

}